import { _decorator, Component, Node, tween, v3, NodeEventType, find, AudioClip, Script } from 'cc';
import { BulletBoomFactory } from './BulletBoomFactory';
import { SysContant } from './constant/SysContant';
import { EnemyFactory } from './EnemyFactory';
import { HeroControl } from './HeroControl';
import { HeroLifeMgr } from './HeroLifeMgr';
import { ScoreMgr } from './ScoreMgr';
import { AudioMgr } from './tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('SysStartGame')
export class SysStartGame extends Component {
    
    @property(Node)
    hero: Node;

    @property(Node)
    btnStart: Node;

    @property(AudioClip)
    bgm:AudioClip

    @property(Node)
    btnOver: Node;
    @property(Node)
    btnRestart: Node;

    
    start() {
        this.btnStart.on(NodeEventType.TOUCH_START, this.startGame, this)
        this.btnOver.on(NodeEventType.TOUCH_START, this.gameover, this)
        this.btnRestart.on(NodeEventType.TOUCH_START, this.restartGame, this)
    }

    update(deltaTime: number) {
        
    }


    startGame() {

        /**
         * 1. 隐藏按钮
         * 2. 移动hero
         * 
         * 3. 注册英雄的操作
         * 
         * 4. 播放背景音乐, 显示静音按钮
         * 
         * 5. 游戏计时
         * 
         * 6. 开始生成敌人
         * 
         * 7. 更新herolife
         * 
         * 8. 生成buffer
         * 
         */

        // 1.
        this.node.active = false

        // 2.
        tween(this.hero).to(1, {position: v3(0, -300)}).start()

        // 3.
        find('Canvas/hero').getComponent(HeroControl).startGame()

        // 4. 
        AudioMgr.inst.play(this.bgm, 0.1)

        find('Canvas/control/voice_Mgr').active = true

        // 5. 
        this.schedule(this.setGameTime, 1)
        SysContant.inst.gameScore = 0
        find('Canvas/control/score').getComponent(ScoreMgr).showScore()

        // 6.
        find('Canvas/enemys').getComponent(EnemyFactory).attact()

        // 7. 
        SysContant.inst.heroLife = SysContant.inst.defHeroLife
        find('Canvas/control/heroLife/life').getComponent(HeroLifeMgr).showHeroLife()
        find('Canvas/control/heroLife').active = true

        // 8
        find('Canvas/battleFiled').getComponent(BulletBoomFactory).generate()
        
    }


    /**
     * 结束游戏
     */
    gameover() {
        /**
         * 1. 隐藏按钮
         * 
         * 2. 显示英雄hero, 位置 >> 位置回归 // 重新开始游戏==> 还在原来位置
         * 
         * 3. 取消英雄的操作
         * 
         * 4. 停止背景音乐, 隐藏静音按钮
         * 
         * 5. 游戏计时取消, 清零
         * 
         * 6. 取消生成敌人
         * 
         * 7. 更新herolife/ 不显示life
         * 
         */
        
        // 1.
        find('Canvas/gameover').active = false
        
        // 显示开始游戏面板
        find('Canvas/start_game').active = true

        // 3. 
        find('Canvas/hero').getComponent(HeroControl).gameOver()

        // 4.
        AudioMgr.inst.stop()
        find('Canvas/control/voice_Mgr').active = false

        // 5.
        this.unschedule(this.setGameTime)
        SysContant.inst.setGameTime(0)

        // 6.
        find('Canvas/enemys').getComponent(EnemyFactory).unattact()
        // 清除所有敌人
        find('Canvas/enemys').getComponent(EnemyFactory).clearAll()

        // 2. 
        this.hero.setPosition(v3(0 , -50))
        this.hero.active = true

        // 7. 
        find('Canvas/control/heroLife/life').getComponent(HeroLifeMgr).showHeroLife()
        find('Canvas/control/heroLife').active = false


    }


    restartGame() {
        this.gameover()

        SysContant.inst.heroLife = SysContant.inst.defHeroLife
        
        SysContant.inst.gameScore = 0
        find('Canvas/control/score').getComponent(ScoreMgr).showScore()


        this.startGame()
    }
    
    
    setGameTime() {
        SysContant.inst.gameTime = 1
    }
}


